<template>
  <div class="app-container">
    <el-button type="warning" @click="add()">新增</el-button>
    <el-button type="warning" @click="update()">修改</el-button>
    <el-button type="warning" @click="del()">删除</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      row-key="menuId"
      border
      lazy
      :tree-props="{children: 'sons', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="menuId"
        label="菜单ID"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="菜单名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="pname"
        label="上级菜单">
      </el-table-column>
      <el-table-column
        prop="icon"
        label="图标">
      </el-table-column>
      <el-table-column
        prop="type"
        label="类型">
        <template v-slot="scope">
          <span v-if="scope.row.type==0">目录</span>
          <span v-if="scope.row.type==1">菜单</span>
          <span v-if="scope.row.type==2">按钮</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="orderNum"
        label="排序号">
      </el-table-column>
      <el-table-column
        prop="url"
        label="菜单URL">
      </el-table-column>
      <el-table-column
        prop="perms"
        label="授权标识">
      </el-table-column>
    </el-table>
    <el-dialog title="保存菜单" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="类型" :label-width="formLabelWidth">
          <el-radio-group v-model="form.type">
            <el-radio :label="0">目录</el-radio>
            <el-radio :label="1">菜单</el-radio>
            <el-radio :label="2">按钮</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <el-form :model="form" v-show="form.type==1">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </el-form-item>
        <el-form-item label="菜单URL" :label-width="formLabelWidth">
          <el-input v-model="form.url" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="授权标识" :label-width="formLabelWidth">
          <el-input v-model="form.perms" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="排序号" :label-width="formLabelWidth">
          <el-input-number v-model="form.orderNum" controls-position="right" @change="handleChange" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth">
          <el-input v-model="form.icon" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form" v-show="form.type==0">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </el-form-item>
        <el-form-item label="排序号" :label-width="formLabelWidth">
          <el-input-number v-model="form.orderNum" controls-position="right" @change="handleChange" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="图标" :label-width="formLabelWidth">
          <el-input v-model="form.icon" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <el-form :model="form" v-show="form.type==2">
        <el-form-item label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="上级菜单" :label-width="formLabelWidth">
          <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
        </el-form-item>
        <el-form-item label="授权标识" :label-width="formLabelWidth">
          <el-input v-model="form.perms" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  created() {
    this.list()
  },
  data() {
    return {
      hasChildren: true,
      tableData: [],
      data:[],
      dialogFormVisible:false,
      formLabelWidth:"120px",
      ids:[],
      form:{
        menuId:"",
        parentId:"",
        name:"",
        url:"",
        perms:"",
        type:"",
        icon:"",
        orderNum:"",
      },
      defaultProps:{
        children: 'sons',
        label:'name'
      }
    }
  },
  methods: {
    list() {
      this.axios.get("http://localhost:80/api/sys/wish/sys-menu/list").then((response) => {
        console.log(response.data)
        this.tableData = response.data.obj
      })
      this.axios.post("http://localhost:80/api/sys/wish/sys-menu/list").then((response)=>{
        this.data=response.data.obj
      })
    },
    add(){
      this.dialogFormVisible=true
      this.flag="add"
      this.form.menuId=""
      this.form.parentId=""
      this.form.name=""
      this.form.url=""
      this.form.perms=""
      this.form.type=""
      this.form.icon=""
      this.form.orderNum=""
    },
    update(data){
      if(this.ids.length>1||this.ids.length<1){
        alert("请选择一条记录!!!")
      }else {
        this.axios.post("http://localhost:80/api/sys/wish/sys-menu/getOne?id="+this.ids[0]).then((response) => {
          console.log(response.data)
          this.form.menuId=response.data[0].menuId
          this.form.parentId=response.data[0].parentId
          this.form.name=response.data[0].name
          this.form.url=response.data[0].url
          this.form.perms=response.data[0].perms
          this.form.type=response.data[0].type
          this.form.icon=response.data[0].icon
          this.form.orderNum=response.data[0].orderNum
          this.dialogFormVisible=true
          this.flag="update"
        })
      }
    },
    handleChange(value) {
      console.log(value);
    },
    save(){
      var url=""
      if(this.flag=="add"){
        url="http://localhost:80/api/sys/wish/sys-menu/add"
      }else if(this.flag=="update"){
        url="http://localhost:80/api/sys/wish/sys-menu/update?id="+this.ids[0]
      }
      this.axios.post(url,this.form).then((response)=>{
        if(response.data){
          alert("成功")
          this.dialogFormVisible=false
          this.list()
        }else {
          alert("失败")
        }
      })
    },
    handleNodeClick(data) {
      console.log(data)
      this.form.parentId=data.menuId
    },
    handleSelectionChange(val) {
      this.ids=new Array()
      for (const i of val){
        if(i.parentId!=0){
          this.ids.push(i.menuId)
        }
      }
    },
    del(){
      if(this.ids.length == 0){
        this.$message({
          message:'不能删除目录',
          type:'warning'
        })
        return
      }
      if(confirm("确定要删除吗？")){
        this.axios.post("http://localhost:80/api/sys/wish/sys-menu/delById?ids="+this.ids).then((response)=>{
          if(response.data){
            alert("删除成功")
            this.list()
          }else {
            alert("删除失败")
          }
        })
      }
    }
  }
}
</script>

